import time,unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys
from unit822.renwu822.a3mall_dy import dy

class Test_01(unittest.TestCase):
    def setUp(self) -> None:
        # 数据库断言1
        self.dd = dy(host='10.59.9.14',username='root',password='yidong666',database='a3mall',post=3306)
        self.driver=webdriver.Edge()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.14:7003/')

    def tearDown(self) -> None:
        time.sleep(3)
        self.driver.quit()

    def test_A3mall_login(self):
        # 登录
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(3)
        # 断言1：断言文本：会员登陆
        dy1 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[1]/span[1]').text
        print("文本断言1，会员登陆:", dy1)
        time.sleep(3)
        # 用户名  密码
        self.driver.find_element(By.NAME, 'username').send_keys("13811033962")
        time.sleep(3)
        self.driver.find_element(By.NAME, 'password').send_keys("123456")
        time.sleep(8)
        # 登录按钮
        self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[5]/a').click()
        time.sleep(5)
        # 断言2：断言文本：欢迎回来，13811033962
        dy2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print("文本断言2，欢迎回来，13811033962:", dy2)
        time.sleep(2)
        # 断言3：断言文本：账户余额：¥ 988389770.25  中的 988389770.25
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]').text
        dy31 = dy3.replace('¥ ', '')
        print("文本断言3，账户余额:", dy31)
        time.sleep(2)
        # 数据库断言 2
        userinfo=self.dd.select("select * from mall_users where username='13811033962'")
        print(userinfo)

    @unittest.skip
    def test_A3mall_goods(self):
        # 调用登录成功用例
        self.test_A3mall_login()
        # 点击首页
        self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[1]/a').click()
        # 输入框输入凤冠
        search = self.driver.find_element(By.XPATH, '//*[@id="searchForm"]/div[1]/input')
        search.send_keys('凤冠')
        search.send_keys(Keys.ENTER)
        # 断言4：搜索凤冠 搜索列表：搜索结果
        dy4 = self.driver.find_element(By.XPATH, '/html/body/div[4]/a[2]').text
        print("文本断言4，搜索结果:", dy4)
        # 选择凤冠
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
        # 断言5：商品详情：断言文本：
        dy5 = self.driver.find_element(By.XPATH, '//*[@id="goodsForm"]/div/div[2]').text
        print("文本断言5，明孝端皇后九龙九凤冠:", dy5)
        # 断言6 商品价格 10.00
        dy6 = self.driver.find_element(By.ID, 'sell-price-box').text
        print("文本断言6，10.00：", dy6)
        # 购物车数量 清除1  输入2
        nums = self.driver.find_element(By.ID, 'store_nums')
        nums.clear()
        nums.send_keys(2)
        # dy7=nums.text
        # print("文本断言7，数量：",dy7)

        # 点击立即购买
        self.driver.find_element(By.XPATH, '//*[@id="buy-cart"]').click()
        time.sleep(3)
        # 断言文本： 断言确认订单
        dy8 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
        print("文本断言8，确认订单:", dy8)
        time.sleep(3)
        # 确认收获地址
        self.driver.find_element(By.CLASS_NAME, 'radio-select-btn').click()
        # 选择支付方式
        self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[3]/div[2]/div[1]/label/input').click()
        # 下一步 支付订单成功
        self.driver.find_element(By.CSS_SELECTOR, '.ask-btn').click()
        # 断言9：提交订单 跳转到订单完成页面 文本断言：订购完成，您的订单支付成功，2025082209472294425
        dy9 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[2]/ul/li[3]/em').text
        print("文本断言9，订购完成:", dy9)
        time.sleep(3)
        dy91 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/span').text
        print("文本断言91，您的订单支付成功:", dy91)
        dy92 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[4]/div[2]/ul/li[1]').text
        print("文本断言92，订单编号:", dy92.replace('订单编号：', ''))
        # 点击查看订单
        self.driver.find_element(By.CLASS_NAME, 'immediate-pay').click()
        # 断言我的订单
        dy10 = self.driver.find_element(By.XPATH, '/html/body/div[4]/a[3]').text
        print("文本断言10，我的订单:", dy10)
        # 断言订单号  2025082210412574583
        dy101 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print("文本断言101，订单号 :", dy101)
        # 断言 订单状态  等待发货
        dy102 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[5]/td[2]').text
        print("文本断言102，订单状态 :", dy102)
        time.sleep(3)
        # 点击申请退款
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[6]/td[2]/div[1]/a').click()

        # 断言  退款详情  订单信息   订单号  运费  订单金额
        dy11 = self.driver.find_element(By.XPATH, '/html/body/div[4]/a[3]').text
        print("文本断言11，退款详情 :", dy11)
        dy111 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[1]/th[1]').text
        print("文本断言111，订单信息 :", dy111)
        dy112 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').click()
        print("文本断言111，订单号 :", dy112)
        # 断言运费   +15.00
        dy113 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[4]/td[2]').text
        print("文本断言113，运费 :", dy113)
        # 断言订单金额  ￥25.00
        dy113 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[5]/td[2]').text
        print("文本断言113，订单金额 :", dy113)
        # 退款说明
        self.driver.find_element(By.XPATH, '//*[@id="theForm"]/table/tbody/tr[1]/td/span/textarea').send_keys(
            "选择更有性价比的商品")
        # 提交
        self.driver.find_element(By.CLASS_NAME, 'ask-btn').click()
        time.sleep(3)


if __name__ == '__main__':
    unittest.main()